package com.zkthink.ceres.member.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.ceres.member.dao.CeresRelationAcoountMapper;
import com.zkthink.ceres.member.dto.RelationAccountSearchDTO;
import com.zkthink.ceres.member.entity.CeresRelationAcoount;
import com.zkthink.ceres.member.service.CeresRelationAcoountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 业务实现类
 * 关联客户表
 * </p>
 *
 * @author ck
 * @date 2022-03-10
 */
@Slf4j
@Service
public class CeresRelationAcoountServiceImpl extends SuperServiceImpl<CeresRelationAcoountMapper, CeresRelationAcoount> implements CeresRelationAcoountService {
    @Override
    public void addRelationAccount(Long mainMid, Long relationMid) {
        //先查询有没有关系
        QueryWrapper<CeresRelationAcoount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("main_mid", mainMid).eq("relation_mid", relationMid);
        CeresRelationAcoount ceresRelationAcoount = baseMapper.selectOne(queryWrapper);
        if (ceresRelationAcoount == null) {
            ceresRelationAcoount = new CeresRelationAcoount();
            ceresRelationAcoount.setMainMid(mainMid);
            ceresRelationAcoount.setRelationMid(relationMid);
            baseMapper.insert(ceresRelationAcoount);
        }
    }

    @Override
    public void deleteRelationAccount(Long mainMid, Long relationId) {
        baseMapper.deleteRelationAccount(mainMid, relationId);
    }

    @Override
    public IPage<RelationAccountSearchDTO> searchRelationAccount(IPage page, Long mid) {
        return baseMapper.searchRelationAccount(page, mid);
    }

    @Override
    public Boolean ifRelationAccount(Long mainMid, Long relationId) {
        List<CeresRelationAcoount> result = baseMapper.selectRealtionAccount(mainMid, relationId);
        if (result != null && result.size() > 0) {
            return true;
        }
        return false;
    }
}
